package cn.szabadsag.user.service.impl;

import cn.szabadsag.base.exception.BusinessException;
import cn.szabadsag.base.service.impl.BaseServiceImpl;
import cn.szabadsag.user.domain.LoginInfo;
import cn.szabadsag.user.domain.dto.UserDto;
import cn.szabadsag.user.mapper.LoginInfoMapper;
import cn.szabadsag.user.service.ILoginInfoService;
import cn.szabadsag.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yangcong
 * @date 2022年03月12日 11:45
 * @since
 */
@Service
public class LoginInfoServiceImpl extends BaseServiceImpl<LoginInfo> implements ILoginInfoService {


    @Autowired
    private LoginInfoMapper loginInfoMapper;


    /**
     * 根据用户登录的账户查询用户登录数据
     * @param userDto
     * @return
     * @throws BusinessException
     */
    @Override
    public LoginInfo loadByAccount(UserDto userDto) throws BusinessException {
        //  根据用户登录的账户查询用户登录数据
        LoginInfo loginInfo = loginInfoMapper.loadByAccount(userDto.getPhone());

        if (loginInfo==null){
            throw new BusinessException("用户名或密码错误");
        }
        //比较密码--用户输入的密码 加盐加密的结果
        String byMd5 = MD5Utils.encrypByMd5(userDto.getPassword() + loginInfo.getSalt());
        if (!byMd5.equals(loginInfo.getPassword())){
            throw new BusinessException("用户名或密码错误");
        }
        return loginInfo;
    }
}
